package com.meishij.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.app.LoaderManager.LoaderCallbacks;

import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;

import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.meishij.R;
import com.meishij.entry.EventBusEntry;
import com.meishij.entry.PersonInfo;

import org.greenrobot.eventbus.EventBus;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

import static android.Manifest.permission.READ_CONTACTS;

/**
 * A login screen that offers login via email/password.
 */
public class Wode_LoginActivity extends AppCompatActivity implements OnClickListener {

    private EditText login_password;
    private EditText login_phone;
    private TextView tv_zhuce;
    private TextView bt_enter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wode_login);
        login_password = ((EditText) findViewById(R.id.tv_login_password));
        login_phone = ((EditText) findViewById(R.id.tv_login_phone));
        tv_zhuce = ((TextView) findViewById(R.id.tv_login_zhuce));
        bt_enter = ((TextView) findViewById(R.id.bt_login_enter));
        tv_zhuce.setOnClickListener(this);
        bt_enter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_zhuce:
                Intent intent = new Intent(Wode_LoginActivity.this, Wode_Zhuce_Activity.class);
                startActivityForResult(intent, 1000);
                break;
            case R.id.bt_login_enter:
                EventBus.getDefault().post(new EventBusEntry("YoungKaKa"));
                finish();
               /* if (!TextUtils.isEmpty(login_password.getText().toString())
                        && !TextUtils.isEmpty(login_phone.getText().toString())) {
                    PersonInfo p2 = new PersonInfo();
                    p2.setName(login_password.getText().toString());
                    p2.setAddress((login_phone.getText().toString()));
                    p2.save(new SaveListener<String>() {
                        @Override
                        public void done(String objectId, BmobException e) {
                            if (e == null) {
                                Toast.makeText(Wode_LoginActivity.this, "创建用户成功", Toast.LENGTH_SHORT).show();
                               *//* Intent intent = new Intent();
                                intent.putExtra("name", "YoungKaka");
                                setResult(RESULT_OK, intent);*//*
                                EventBus.getDefault().post(new EventBusEntry("YoungKaKa"));
                                finish();
                            } else {
                                Toast.makeText(Wode_LoginActivity.this, "创建用户失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }*/
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            if (resultCode == RESULT_OK) {
                String phone = data.getStringExtra("phone");
                login_phone.setText(phone);
            }
        }
    }
}

